include:
  - local: .gitlab/ci/global.gitlab-ci.yml
  - local: .gitlab/ci/qa-common/main.gitlab-ci.yml
  - local: .gitlab/ci/qa-common/rules.gitlab-ci.yml
  - local: .gitlab/ci/qa-common/variables.gitlab-ci.yml
  - component: "gitlab.com/gitlab-org/quality/pipeline-common/allure-report@7.3.0"
    inputs:
      job_name: "e2e-test-report"
      job_stage: "report"
      aws_access_key_id_variable_name: "QA_ALLURE_AWS_ACCESS_KEY_ID"
      aws_secret_access_key_variable_name: "QA_ALLURE_AWS_SECRET_ACCESS_KEY"
      gitlab_auth_token_variable_name: "PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE"
      allure_results_glob: "qa/tmp/allure-results"
      allure_ref_slug: "${CI_COMMIT_REF_SLUG}"
      allure_project_path: "${CI_PROJECT_PATH}"
      allure_merge_request_iid: "${CI_MERGE_REQUEST_IID}"
      allure_job_name: "${QA_RUN_TYPE}"

variables:
  COLORIZED_LOGS: "true"
  GIT_DEPTH: "20"
  GIT_STRATEGY: "clone" # 'GIT_STRATEGY: clone' optimizes the pack-objects cache hit ratio
  GIT_SUBMODULE_STRATEGY: "none"

.gdk-qa-base:
  image: ${REGISTRY_HOST}/${REGISTRY_GROUP}/gitlab-build-images/debian-${DEBIAN_VERSION}-ruby-${RUBY_VERSION}:bundler-2.3-git-2.36-lfs-2.9-chrome-${CHROME_VERSION}-docker-${DOCKER_VERSION}-gcloud-383-kubectl-1.23
  extends:
    - .qa-cache
  stage: test
  services:
    - docker:${DOCKER_VERSION}-dind
  tags:
    - e2e
  variables:
    DOCKER_HOST: tcp://docker:2375
    QA_GENERATE_ALLURE_REPORT: "true"
    QA_CAN_TEST_PRAEFECT: "false"
    QA_INTERCEPT_REQUESTS: "false"
    GITLAB_QA_ADMIN_ACCESS_TOKEN: $QA_ADMIN_ACCESS_TOKEN
    RSPEC_REPORT_OPTS: "--order random --force-color --format documentation --format RspecJunitFormatter --out tmp/rspec-${CI_JOB_ID}.xml --format QA::Support::JsonFormatter --out tmp/rspec-${CI_JOB_ID}.json  --format html --out tmp/rspec-${CI_JOB_ID}.htm"
    FF_NETWORK_PER_BUILD: 1
    GDK_URL: http://gdk.test:3000
    KNAPSACK_TEST_FILE_PATTERN: "qa/specs/features/**/*_spec.rb"
  before_script:
    - echo "SUITE_RAN=true" > suite_status.env
    - echo -e "\e[0Ksection_start:`date +%s`:pull_image\r\e[0KPull GDK QA image"
    - docker pull ${GDK_IMAGE}
    - echo -e "\e[0Ksection_end:`date +%s`:pull_image\r\e[0K"
    # Despite `incremental: false` and `static: true`, GDK sometimes fails to start without increasing max user watches
    # This is why we're not running the GDK container as a service
    - sysctl -n -w fs.inotify.max_user_watches=524288
    - echo -e "\e[0Ksection_start:`date +%s`:launch_gdk\r\e[0KLaunch GDK"
    - mkdir -p $CI_PROJECT_DIR/log/gdk $CI_PROJECT_DIR/log/gitlab
    # This command matches the permissions of the user that runs GDK inside the container.
    - chown -R 1000:1000 $CI_PROJECT_DIR/log
    - |
      docker run -d --rm --name gdk --network host \
        --volume $CI_PROJECT_DIR/log/gdk:/home/gdk/gitlab-development-kit/log \
        --volume $CI_PROJECT_DIR/log/gitlab:/home/gdk/gitlab-development-kit/gitlab/log \
        ${GDK_IMAGE}
    # With `FF_NETWORK_PER_BUILD=1` and `--network host` the IP of the gdk container should be 172.18.0.2, but we get it
    # dynamically just in case
    - echo "$(docker exec gdk bash -c "getent hosts \$HOSTNAME" | awk '{print $1}') gdk.test" >> /etc/hosts
    - source scripts/utils.sh
    - cd qa && bundle install
  script:
    - retry_exponential test_url $GDK_URL/users/sign_in
    - echo -e "\e[0Ksection_end:`date +%s`:launch_gdk\r\e[0K"
    - echo -e "\e[0Ksection_start:`date +%s`:run_tests\r\e[0KRun E2E tests"
    - export QA_COMMAND="bundle exec bin/qa ${QA_SCENARIO:=Test::Instance::All} $GDK_URL $GITLAB_QA_OPTS -- $QA_TESTS $QA_RSPEC_TAGS $RSPEC_REPORT_OPTS"
    - echo "Running - '$QA_COMMAND'"
    - eval "$QA_COMMAND"
    - echo -e "\e[0Ksection_end:`date +%s`:run_tests\r\e[0K"
  after_script:
    - |
      if [ "$CI_JOB_STATUS" == "failed" ]; then
        echo "SUITE_FAILED=true" >> suite_status.env
      fi
    - docker stop gdk
  artifacts:
    paths:
      - qa/tmp
      - log
    reports:
      junit: qa/tmp/rspec-*.xml
      dotenv: suite_status.env
    expire_in: 7 days
    when: always

download-knapsack-report:
  extends:
    - .download-knapsack-report
    - .rules:download-knapsack

cache-gems:
  extends:
    - .ruby-image
    - .qa-cache-push
    - .rules:update-cache
  stage: .pre
  tags:
    - e2e
  script:
    - cd qa && bundle install

# Take the existing GDK docker image and reconfigure it with Postgres load
# balancing. Adding 5s lag to 1 of the replicas to validate robustness of
# the load balancer.
.gdk-with-load-balancer-setup:
  before_script:
    - !reference [".gdk-qa-base", "before_script"]
    - |
      docker exec gdk bash -c "
        gdk config set postgresql.replica.enabled true &&\
        gdk config set postgresql.replica_2.enabled true &&\
        gdk config set load_balancing.enabled true &&\
        gdk reconfigure &&\
        gdk restart"

download-fast-quarantine-report:
  extends:
    - .download-fast-quarantine-report
    - .rules:download-fast-quarantine-report

gdk-qa-smoke:
  extends:
    - .gdk-qa-base
  variables:
    QA_SCENARIO: Test::Instance::Smoke
    QA_RUN_TYPE: gdk-qa-smoke
  rules:
    - when: always

gdk-qa-smoke-with-load-balancer:
  extends:
    - .gdk-qa-base
    - .gdk-with-load-balancer-setup
  variables:
    QA_SCENARIO: Test::Instance::Smoke
    QA_RUN_TYPE: gdk-qa-smoke
  artifacts:
    paths:
      - log
    reports:
      dotenv: ""
  rules:
    - changes:
        - ".gitlab/ci/test-on-gdk/**"
        - "lib/gitlab/database/load_balancing/**/*"
  allow_failure: true

gdk-qa-reliable:
  extends:
    - .gdk-qa-base
    - .parallel
  variables:
    QA_SCENARIO: Test::Instance::Blocking
    QA_RUN_TYPE: gdk-qa-blocking
  rules:
    - when: always
  allow_failure: true

gdk-qa-reliable-with-load-balancer:
  extends:
    - .gdk-qa-base
    - .gdk-with-load-balancer-setup
    - .parallel
  variables:
    QA_SCENARIO: Test::Instance::Blocking
    QA_RUN_TYPE: gdk-qa-blocking
  artifacts:
    paths:
      - log
    reports:
      dotenv: ""
  rules:
    - changes:
        - ".gitlab/ci/test-on-gdk/**"
        - "lib/gitlab/database/load_balancing/**/*"
  allow_failure: true

gdk-qa-non-blocking:
  extends:
    - .gdk-qa-base
    - .parallel
  variables:
    QA_SCENARIO: Test::Instance::NonBlocking
    QA_RUN_TYPE: gdk-qa-non-blocking
  rules:
    - when: manual
  allow_failure: true

# ==========================================
# Post test stage
# ==========================================
e2e-test-report:
  extends: .rules:report:allure-report

upload-knapsack-report:
  extends:
    - .upload-knapsack-report
    - .rules:report:process-results
  variables:
    QA_KNAPSACK_REPORT_FILE_PATTERN: $CI_PROJECT_DIR/qa/tmp/knapsack/*/*.json

export-test-metrics:
  extends:
    - .export-test-metrics
    - .rules:report:process-results
  variables:
    QA_METRICS_REPORT_FILE_PATTERN: $CI_PROJECT_DIR/qa/tmp/test-metrics-*.json

.gitlab-qa-report:
  variables:
    QA_RSPEC_JSON_FILE_PATTERN: $CI_PROJECT_DIR/qa/tmp/rspec-*.json
    QA_SYSTEM_LOG_FILE_PATTERN: $CI_PROJECT_DIR/test_output/logs

generate-test-session:
  extends:
    - .generate-test-session
    - .rules:report:process-results
  variables:
    QA_RSPEC_JSON_FILE_PATTERN: $CI_PROJECT_DIR/qa/tmp/rspec-*.json

notify-slack:
  extends:
    - .notify-slack
    - .rules:report:process-results
  variables:
    QA_RSPEC_XML_FILE_PATTERN: $CI_PROJECT_DIR/qa/tmp/rspec-*.xml
